//
//  YWBuildingManager.m
//  maintain
//
//  Created by 郑想 on 15/10/29.
//  Copyright © 2015年 ewin. All rights reserved.
//

#import "YWApartment.h"
#import "YWBuilding.h"
#import "YWBuildingManager.h"
#import "YWDBTool.h"
#import "YWEquipment.h"
#import "YWFloor.h"
#import "YWHandler.h"
#import "YWLocation.h"
#import "YWBuildingJson.h"

@implementation YWBuildingManager

YWSingletonM(Manager)

#pragma mark - bulids methods

- (void)savebuildingJsonWithArray : (NSArray*)buildingJsonArray{
    [[YWDBTool shareDBTool] insertbuildingWithArray:buildingJsonArray];
    [[YWDBTool shareDBTool] insertApartmentWithArray:buildingJsonArray];
    [[YWDBTool shareDBTool] insertFloorWithArray:buildingJsonArray];
    [[YWDBTool shareDBTool] insertLocationWithArray:buildingJsonArray];
}

- (void)updatebuildingJsonWithArray:(NSArray*)buildingJsonArray{
    //因为buildingJsonArray里没有json文件里两个时间，所以这里是更新而不是insert
    if (buildingJsonArray.count) {
        [[YWDBTool shareDBTool] updatebuildingWithJsonArray:buildingJsonArray];
    }
}

- (YWBuilding*)buildingWithId:(NSString*)buildingId
{
    return [[YWDBTool shareDBTool] buildingWithId:buildingId];
}

- (NSString*)allBuildingId
{
    NSMutableString* buildingIds = [NSMutableString string];
    NSArray* buildings = [[YWDBTool shareDBTool] allBuilding];
    [buildings enumerateObjectsUsingBlock:^(YWBuilding* _Nonnull building, NSUInteger idx, BOOL* _Nonnull stop) {
        if (idx != 0) {
            [buildingIds appendString:@","];
        }
        [buildingIds appendString:building.buildingId];
    }];

    return buildings.count ? buildingIds : nil;
}

- (NSArray *)allBuilding{
    return [[YWDBTool shareDBTool] allBuilding];
}

- (YWBuilding*)buildingWithLocationId:(NSInteger)locationId
{
    return [[YWDBTool shareDBTool] buildingWithLocationId:locationId];
}

- (YWBuilding*)buildingWithLocations:(NSArray*)locations
{
    if (locations.count) {
        YWLocation* location = locations[0];
        return [self buildingWithLocationId:location.locationId];
    }
    return nil;
}

- (NSArray*)apartmentWithbuildingId:(NSString*)buildingId
{
    return [[YWDBTool shareDBTool] apartmentsWithbuildingId:buildingId];
}

- (NSArray*)floorWithApartmentId:(NSInteger)apartmentId
{
    return [[YWDBTool shareDBTool] floorsWithApartmentId:apartmentId];
}

- (NSArray*)locationWithFloorId:(NSInteger)floorId
{
    return [[YWDBTool shareDBTool] locationsWithFloorId:floorId];
}

- (NSArray *)locationWithBuildingId:(NSString *)buildingId apartmentId:(NSInteger)apartmentId floorId:(NSInteger)floorId page:(NSInteger)page{
    return [[YWDBTool shareDBTool] locationWithBuildingId:buildingId apartmentId:apartmentId floorId:floorId page:page];
}

- (NSArray *)buildingArrayWithLineId:(NSInteger)lineId type:(BOOL)isKeepWatch lineType:(BOOL)isEquipmentLine{
    return [[YWDBTool shareDBTool] buildingArrayWithLineId:lineId type:isKeepWatch lineType:isEquipmentLine];
}

- (NSArray *)apartmentArrayWithBuildingId:(NSString*)buildingId LineId:(NSInteger)lineId type:(BOOL)isKeepWatch lineType:(BOOL)isEquipmentLine{
    return [[YWDBTool shareDBTool] apartmentArrayWithBuildingId:buildingId LineId:lineId type:isKeepWatch lineType:isEquipmentLine];
}

- (NSArray *)floorArrayWithApartmentId:(NSInteger)apartmentId LineId:(NSInteger)lineId type:(BOOL)isKeepWatch lineType:(BOOL)isEquipmentLine{
    return [[YWDBTool shareDBTool] floorArrayWithApartmentId:apartmentId LineId:lineId type:isKeepWatch lineType:isEquipmentLine];
}

- (NSArray *)locationArrayWithfloorId:(NSInteger)floorId LineId:(NSInteger)lineId type:(BOOL)isKeepWatch lineType:(BOOL)isEquipmentLine{
    return [[YWDBTool shareDBTool] locationArrayWithfloorId:floorId LineId:lineId type:isKeepWatch lineType:isEquipmentLine];
}

- (NSArray *)selectPositionWithMissionId:(NSInteger)missionId equipmentTypeId:(NSInteger)equipmentTypeId positionType:(NSInteger)positionType buildingId:(NSString *)buildingId apartmentId:(NSInteger)apartmentId floorId:(NSInteger)floorId{
    return [[YWDBTool shareDBTool] selectPositionWithMissionId:missionId equipmentTypeId:equipmentTypeId positionType:positionType buildingId:buildingId apartmentId:apartmentId floorId:floorId];
}

- (YWLocation*)locationWithId:(NSInteger)locationId
{
    return [[YWDBTool shareDBTool] locationWithId:locationId];
}
- (YWFloor*)floorWithId:(NSInteger)floorId
{
    return [[YWDBTool shareDBTool] floorWithId:floorId];
}
- (YWApartment*)apartmentWithId:(NSInteger)apartmentId
{
    return [[YWDBTool shareDBTool] apartmentWithId:apartmentId];
}

- (YWBuilding*)buildingWithEquipmentId:(NSString*)equipmentId
{
    YWEquipment* equipment = [[YWDBTool shareDBTool] equipmentWithId:equipmentId];
    return [self buildingWithLocationId:equipment.locationId];
}

- (NSString*)equipmentLocation:(YWEquipment*)equipment
{
    YWBuilding* building = [[YWDBTool shareDBTool] buildingWithId:equipment.buildingId];
    return [NSString stringWithFormat:@"%@%@", building.buildingName ?: @"", [self equipmentLocationNobuilding:equipment]];
}

- (NSString*)equipmentLocationNobuilding:(YWEquipment*)equipment
{
    YWApartment* apartment = [[YWDBTool shareDBTool] apartmentWithId:equipment.apartmentId];
    YWFloor* floor = [[YWDBTool shareDBTool] floorWithId:equipment.floorId];
    YWLocation* location = [[YWDBTool shareDBTool] locationWithId:equipment.locationId];
    return [NSString stringWithFormat:@"%@%@%@", apartment.apartmentName ?: @"", floor.floorName ?: @"", location.locationName ?: @""];
}

- (NSString*)areaLocation:(NSArray*)locations
{
    YWBuilding* building = [self buildingWithLocations:locations];
    return [NSString stringWithFormat:@"%@%@", building.buildingName ?: @"", [self areaLocationNobuilding:locations]];
}

- (NSString*)areaLocationNobuilding:(NSArray*)locations
{
    if (locations.count) {
        YWLocation* location = locations[0];
        YWFloor* floor = [[YWDBTool shareDBTool] floorWithId:location.floorId];
        YWApartment* apartment = [[YWDBTool shareDBTool] apartmentWithId:floor.apartmentId];
        return [NSString stringWithFormat:@"%@%@%@", apartment.apartmentName ?: @"", floor.floorName ?: @"", location.locationName ?: @""];
    }
    return nil;
}

- (NSString*)areaLocationNobuildingWithId:(NSInteger)locationId
{
    YWLocation* location = [[YWDBTool shareDBTool] locationWithId:locationId];
    YWFloor* floor = [[YWDBTool shareDBTool] floorWithId:location.floorId];
    YWApartment* apartment = [[YWDBTool shareDBTool] apartmentWithId:floor.apartmentId];
    return [NSString stringWithFormat:@"%@%@%@", apartment.apartmentName ?: @"", floor.floorName ?: @"", location.locationName ?: @""];
}

- (NSString*)areaLocationWithLocationId:(NSInteger)locationId{
    YWBuilding *building = [self buildingWithLocationId:locationId];
    NSString *location = [NSString stringWithFormat:@"%@%@",building.buildingName?:@"",[self areaLocationNobuildingWithId:locationId]];
    return location;
}

@end
